<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

//@TODO hacer una libreria para tratar los settings
//Autocarga y almacena en cache o similar los settings.
//Almacenara tb opciones fallidas para no sobrecargar las consultas... wp-includes/cache.php
class Setting_Model extends Model {
	var $_table = "settings";
	
    function __construct() {
        parent::Model();
    }
    
    function get($key = NULL){
    	if($key == NULL){
    		$this->db->from($this->_table);
    		$this->db->where("autoload", 1);
    		
    		$query = $this->db->get ();
    		
    		return $query->result();
    	} else{
	    	$this->db->select("setting_value");
	    	$this->db->from($this->_table);
	    	$this->db->where("setting_key", $key);
	    	
	    	$query = $this->db->get ();
	    	
	    	if ($query->num_rows() > 0){
				return $query->row();
			}	
    	}
    	
		return FALSE;
    }
    
    function update($key = "", $value = "", $autoload = NULL){
    	if($this->get($key) !== FALSE){
	    	$data = array(
	    		"setting_value" => $value
	    	);
	    	
	    	if($autoload !== NULL){
	    		$data['autoload'] = $autoload?1:0;
	    	}
	    	
	    	$this->db->update($this->_table, $data, array('setting_key'=>$key));
    	} else{
    		$data = array(
    			"setting_key"	=> $key,
	    		"setting_value" => $value
	    	);
	    	
	    	$this->db->insert($this->_table, $data);
    	}
		return TRUE;
    }
    
	function delete($key = ""){
    	$this->db->where('setting_key', $key);			
		$this->db->delete($this->_table);
    }
    
}

?>